package com.atguigu.spzx.common.feigninter;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

//RequestInterceptor，feign的拦截器接口，在openfeign包下的，所以需要添加openfeign的依赖
//<dependency>
//<groupId>org.springframework.cloud</groupId>
//<artifactId>spring-cloud-starter-openfeign</artifactId>
//</dependency>
public class FeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();//上一个请求对应的request对象
        String token = request.getHeader("token");

        //向feign的请求中添加一个字段
        requestTemplate.header("token",token);
    }
}
